{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "class Homework(object):\n",
    "    def __init__(self):\n",
    "        self._grade = 0\n",
    "\n",
    "    @property\n",
    "    def grade(self):\n",
    "        return self._grade\n",
    "\n",
    "    @grade.setter\n",
    "    def grade(self, value):\n",
    "        if not (0 <= value <= 100):\n",
    "            raise ValueError('Grade must be between 0 and 100')\n",
    "        self._grade = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "95\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "galileo = Homework()\n",
    "galileo.grade = 95\n",
    "print(galileo.grade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 3\n",
    "class Exam(object):\n",
    "    def __init__(self):\n",
    "        self._writing_grade = 0\n",
    "        self._math_grade = 0\n",
    "\n",
    "    @staticmethod\n",
    "    def _check_grade(value):\n",
    "        if not (0 <= value <= 100):\n",
    "            raise ValueError('Grade must be between 0 and 100')\n",
    "\n",
    "\n",
    "# Example 4\n",
    "    @property\n",
    "    def writing_grade(self):\n",
    "        return self._writing_grade\n",
    "\n",
    "    @writing_grade.setter\n",
    "    def writing_grade(self, value):\n",
    "        self._check_grade(value)\n",
    "        self._writing_grade = value\n",
    "\n",
    "    @property\n",
    "    def math_grade(self):\n",
    "        return self._math_grade\n",
    "\n",
    "    @math_grade.setter\n",
    "    def math_grade(self, value):\n",
    "        self._check_grade(value)\n",
    "        self._math_grade = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing:    85\n",
      "Math:       99\n"
     ]
    }
   ],
   "source": [
    "# Example 5\n",
    "galileo = Exam()\n",
    "galileo.writing_grade = 85\n",
    "galileo.math_grade = 99\n",
    "print('Writing: %5r' % galileo.writing_grade)\n",
    "print('Math:    %5r' % galileo.math_grade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 6\n",
    "class Grade(object):\n",
    "    def __get__(*args, **kwargs):\n",
    "        pass\n",
    "\n",
    "    def __set__(*args, **kwargs):\n",
    "        pass\n",
    "\n",
    "class Exam(object):\n",
    "    # Class attributes\n",
    "    math_grade = Grade()\n",
    "    writing_grade = Grade()\n",
    "    science_grade = Grade()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 7\n",
    "exam = Exam()\n",
    "exam.writing_grade = 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 8\n",
    "Exam.__dict__['writing_grade'].__set__(exam, 40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# Example 9\n",
    "print(exam.writing_grade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# Example 10\n",
    "print(Exam.__dict__['writing_grade'].__get__(exam, Exam))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 11\n",
    "class Grade(object):\n",
    "    def __init__(self):\n",
    "        self._value = 0\n",
    "\n",
    "    def __get__(self, instance, instance_type):\n",
    "        return self._value\n",
    "\n",
    "    def __set__(self, instance, value):\n",
    "        if not (0 <= value <= 100):\n",
    "            raise ValueError('Grade must be between 0 and 100')\n",
    "        self._value = value\n",
    "\n",
    "class Exam(object):\n",
    "    math_grade = Grade()\n",
    "    writing_grade = Grade()\n",
    "    science_grade = Grade()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing 82\n",
      "Science 99\n"
     ]
    }
   ],
   "source": [
    "# Example 12\n",
    "first_exam = Exam()\n",
    "first_exam.writing_grade = 82\n",
    "first_exam.science_grade = 99\n",
    "print('Writing', first_exam.writing_grade)\n",
    "print('Science', first_exam.science_grade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Second 75 is right\n",
      "First  75 is wrong\n"
     ]
    }
   ],
   "source": [
    "# Example 13\n",
    "second_exam = Exam()\n",
    "second_exam.writing_grade = 75\n",
    "print('Second', second_exam.writing_grade, 'is right')\n",
    "print('First ', first_exam.writing_grade, 'is wrong')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 14\n",
    "class Grade(object):\n",
    "    def __init__(self):\n",
    "        self._values = {}\n",
    "\n",
    "    def __get__(self, instance, instance_type):\n",
    "        if instance is None: return self\n",
    "        return self._values.get(instance, 0)\n",
    "\n",
    "    def __set__(self, instance, value):\n",
    "        if not (0 <= value <= 100):\n",
    "            raise ValueError('Grade must be between 0 and 100')\n",
    "        self._values[instance] = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 15\n",
    "from weakref import WeakKeyDictionary\n",
    "\n",
    "class Grade(object):\n",
    "    def __init__(self):\n",
    "        self._values = WeakKeyDictionary()\n",
    "    def __get__(self, instance, instance_type):\n",
    "        if instance is None: return self\n",
    "        return self._values.get(instance, 0)\n",
    "\n",
    "    def __set__(self, instance, value):\n",
    "        if not (0 <= value <= 100):\n",
    "            raise ValueError('Grade must be between 0 and 100')\n",
    "        self._values[instance] = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First  82 is right\n",
      "Second 75 is right\n"
     ]
    }
   ],
   "source": [
    "# Example 16\n",
    "class Exam(object):\n",
    "    math_grade = Grade()\n",
    "    writing_grade = Grade()\n",
    "    science_grade = Grade()\n",
    "\n",
    "first_exam = Exam()\n",
    "first_exam.writing_grade = 82\n",
    "second_exam = Exam()\n",
    "second_exam.writing_grade = 75\n",
    "print('First ', first_exam.writing_grade, 'is right')\n",
    "print('Second', second_exam.writing_grade, 'is right')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
